package com.everflourish.yeah100.http;

import android.widget.ImageView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.everflourish.yeah100.R;
import com.everflourish.yeah100.utils.BitmapCache;
import com.everflourish.yeah100.utils.Yeah100;

/**
 * 图片的请求
 *
 * @author pf
 * @date 2015-1-14
 */
public class ImageRequest extends BaseRequest {
    private static ImageRequest mRequest;
    private ImageLoader mLoader;

    private ImageRequest(RequestQueue queue) {
        mLoader = new ImageLoader(queue, new BitmapCache());
    }

    public static void init() {
        mRequest = null;
    }

    public static ImageRequest getInstance(RequestQueue queue) {
        if (mRequest == null) {
            mRequest = new ImageRequest(queue);
        }
        return mRequest;
    }

    /**
     * 用户头像请求
     *
     * @param iv
     * @param url
     */
    public void get(ImageView iv, String url) {
//        mLoader.setHeaders(new BaseRequest().addRequestHeader());
        ImageListener imageListener = ImageLoader.getImageListener(iv,
                R.mipmap.question_image_normal, R.mipmap.question_image_normal);
        if (url != null) {
            mLoader.get(url, imageListener);
        }
    }

    /**
     * 用户头像请求
     *
     * @param iv
     */
    public void get(ImageView iv) {
        mLoader.setHeaders(new BaseRequest().addRequestHeader());
        ImageListener imageListener = ImageLoader.getImageListener(iv,
                R.mipmap.head, R.mipmap.head);
        mLoader.get(Yeah100.userImageUrl, imageListener);
    }
}